package demo.axitdn;

import java.util.ArrayList;
import java.util.List;

import demo.axitdn.model.DemoModel;
import demo.axitdn.model.Model;
import demo.axitdn.physic.Accumulator;
import demo.axitdn.physic.CircularMotion;
import demo.axitdn.physic.GravityEffect;
import demo.axitdn.physic.SpringEffect;
import demo.axitdn.physic.State;
import demo.axitdn.physic.Vector3f;
import demo.axitdn.physic.ZeroForce;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView {
	private Bitmap bmp;
	private SurfaceHolder holder;
	private GameLoopThread gameLoopThread;
	// Constants
    //1m=30px;
    float x0 = 3;//m
    float y0 = 9;//m
    float v0 = 3.0f;// initial vel, m/s
    float vx0=100;
    float vy0=0;
    float angle = (float) (-Math.PI / 3.0f);
    double t;
    float dt = 0.009f;
    int nSteps = 100000;
    DemoModel demo;
    DemoModel demo2;
    DemoModel demo3;
    List<Model> list;
    CircularMotion cm;
    /** Set up for the algorithm. **/
    public void init() {
        t = 0.0f;
        demo=new DemoModel(x0,y0,2,angle,t,2);
        cm=new CircularMotion(demo.getState().position,demo.getState().velocity,new Vector3f(x0,y0-3,0),5f, dt, 5);
		demo.setForce(cm);
        demo2=new DemoModel(x0,y0+2,0,0,t,10);
        demo2.setForce(new SpringEffect());
        demo3=new DemoModel(x0, y0, 0, angle, t,14);
        demo3.setForce(new ZeroForce());
        list=new ArrayList<Model>();
        list.add(demo);
        list.add(demo2);
        list.add(demo3);
    } // init

	public GameView(Context context) {
		super(context);
		init();
		gameLoopThread = new GameLoopThread(this);
		holder = getHolder();
		holder.addCallback(new SurfaceHolder.Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				boolean retry = true;
				gameLoopThread.setRunning(false);
				while (retry) {
					try {
						gameLoopThread.join();
						retry = false;
					} catch (InterruptedException e) {
					}
				}
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				gameLoopThread.setRunning(true);
				gameLoopThread.start();
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
			}
		});
		bmp = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		//canvas.drawColor(Color.GREEN);
		canvas.drawColor(Color.BLACK);
		Paint p=new Paint();
		p.setColor(Color.GREEN);
		
		canvas.drawCircle(cm.getR().x, cm.getR().y, 2, p);	
		for(Model m:list) {
			m.onDraw(canvas);
		}
	}

	public void onUpdate(Accumulator acc) {
		
		for(Model m:list) {
			m.onUpdate(t);
		}
		
		while (acc.accumulator >= dt) {
            acc.accumulator -= dt;      
            
            for(Model m:list) {
            	m.integrate(t, dt);
    		}
            
            t += dt;
            if(t==Double.MAX_VALUE) {
            	t=0;
            }
        }
        double alpha = (acc.accumulator / dt);
        for(Model m:list) {
        	m.interpolate(alpha);
		}
        
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent evt) {
		// TODO Auto-generated method stub
		int x=(int)evt.getX();
		int y=(int)evt.getY();
		switch(evt.getAction()) {
		case MotionEvent.ACTION_DOWN:
			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			break;
		}		
		return true;
	}
	
	
}
